(function(app){
	
	app.action(['/familyMember'], 'familyMember/familyMember.html', ['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
		function($scope, $route, _API, $location, $timeout, $rootScope) {
			
			//获取好友列表
			function reload(){
				_API.ajax('person/contact/list',{})
				.success(function(json){
					$scope.data = json.result;
				})
				.error(function(json){
					if(json.code == 201){
						$location.path('/certification');
						alert("请登陆手机端，完成实名认证！");
					}
				});
			}
			reload();
			//删除
			$scope.del = function(CA001,CA003,CA002){
				
				if(CA002 == 0){
					var result = confirm('确认删除用户：'+CA003+'?');
					if(result) {
						_API.ajax('myAccount/familyMember/delRC03ById', {"CA001": CA001})
						.success(function(json){
							if(json.result.isPass == true){
								alert("删除成功");
								reload();
							} else {
								alert("删除失败，请联系管理员");
							}
						});
					}
				} else {
					alert("警告：不能删除本人");
				}
				
				
	    	}
			//修改
			$scope.upd = function(d){
//				sessionStorage.setItem("CA003",d.CA003);//姓名                     
//				sessionStorage.setItem("CA006",d.CA006);//身份证
//				sessionStorage.setItem("CA051",d.CA051);//民族
//				sessionStorage.setItem("CA050",d.CA050);//性别
//				sessionStorage.setItem("CA001",d.CA001);//自然人id
//				sessionStorage.setItem("AAE006",d.AAE006);//常住地址
//				sessionStorage.setItem("AAE005",d.AAE005);//联系电话
//				sessionStorage.setItem("insured",d.insured);//是否参保
//				sessionStorage.setItem("CA009",d.CA009);//生日
				var str = d.CA003+","+d.CA006+","+d.CA051+","+d.CA050+","+d.CA001+","+d.AAE006+","+d.AAE005+","+d.AAE0160+","+d.CA009;
				$location.path('/updFamilyMember/'+str);
	    	}
			
		}
	]);
	//修改
	app.action(['/updFamilyMember/:str'], 'familyMember/updFamilyMember.html', ['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
		function($scope, $route, _API, $location, $timeout, $rootScope) {
			var str = $route.current.params.str;
			var info = str.split(",");
			$scope.CA003 = info[0];
			$scope.CA006 = info[1];
			$scope.CA051 = info[2];
			$scope.CA050 = info[3];
			$scope.AAE006 = info[5];
			$scope.AAE005 = info[6]=="undefined"?"":info[6];
			$scope.CA009 = info[8];
			$('#updateFamily').click(function() {
				if(!$("#form").valid()) {
					return false;
				}
				var AAE005 = document.getElementById("AAE005").value;
				var AAE006 = document.getElementById("AAE006").value;
				var CA001 = info[4];
				var insured = info[7];
				
				_API.ajax('myAccount/familyMember/updateInfo', {"insured": insured,"CA001":CA001,"AAE005":AAE005,"AAE006":AAE006})
				.success(function(json){
					alert("您的信息修改申请已成功提交，请等待工作人员审核！");
					$location.path('/familyMember');
				});
			})
		}
	]);
	//新增
	app.action(['/familyMember/create'], 'familyMember/edit.html', ['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
	function($scope, $route, _API, $location, $timeout, $rootScope) {
			
			var upload = $SDC.apiDomain + "/myAccount/familyMember/scanImg.json";
			var shade = document.getElementById('shade'); //上传文件时页面形成遮罩
			$scope.submit = function() {
				var fileObj = document.getElementById("exampleInputFile").files[0]; // 获取文件对象
				// var fileSize = fileObj.size;
				// var size = fileSize / 1024;    
				// if(size>500){  
				//    alert("附件不能大于500k");
				//    return;
				// }
				var form = new FormData();
				form.append("img", fileObj); // 文件对象    
				form.append("appId","6103_web");
				form.append("sig","6103_web");
				shade.style.display = 'block';
				$.ajax({
					"url": upload,
					"dataType": "JSON",
					"data": form,
					"type": "POST",
					"cache": false,
					"timeout": 30000,
					"xhrFields": {
						"withCredentials": true
					},
					"crossDomain": true,
					processData: false,
					contentType: false,
					success: function(json) {
						if(json.result.isPass == false){
							document.fileForm.reset();
							shade.style.display = 'none';
							alert(json.result.info);
							location.hash ="/familyMember";
							return;
						}
						if(json.result.isPass == true){
//							sessionStorage.setItem("CA003",json.result.CA003);//姓名
//							sessionStorage.setItem("CA006",json.result.CA006);//身份证
//							sessionStorage.setItem("CA051",json.result.CA012);//民族
//							sessionStorage.setItem("CA050",json.result.CA013);//性别
//							sessionStorage.setItem("CA009",json.result.CA009);//生日
							var str = json.result.CA003 +","+ json.result.CA006 +","+ json.result.CA051 +","+ json.result.CA050 +","+json.result.CA009;
							location.hash ="/newFamilyMember/"+str;
						}
						// document.fileForm.reset();
						shade.style.display = 'none';
					},
					error: function(json, status) {
						if(status == 'timeout') { // 超时,status还有success,error等值的情况
							error({
								code: -1001,
								msg: 'Ajax timeout'
							});
						} else {
							error({
								code: -1000,
								msg: 'Ajax error'
							});
						}
						document.fileForm.reset();
						shade.style.display = 'none';
					}
				});
			}
	}]);
	
	app.action(['/newFamilyMember/:str'], 'familyMember/newFamilyMember.html', ['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
		function($scope, $route, _API, $location, $timeout, $rootScope) {
			var str = $route.current.params.str;
			var info = str.split(",");
			$scope.CA003 = info[0];
			$scope.CA006 = info[1];
			$scope.CA051 = info[2];
			$scope.CA050 = info[3];
			$scope.CA009 = info[4];
			
			$('#addFamily').click(function() {
				if(!$("#form").valid()) {
					return false;
				}
				var CA008 = document.getElementById("CA008").value;
				_API.ajax('myAccount/familyMember/saveRC03', {"CA008": CA008})
				.success(function(json){
					if(json.result.isPass == true){
						alert("添加成功");
					} else {
						alert("添加失败");
					}
					$location.path('/familyMember');
				});
			})
		}
	]);
	
})($SDC.app);